package org.itboys.core.service.mongo.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.entity.lasted.SystemUser;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.ErrorException;
import org.itboys.core.common.ExceptionEnum;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.entity.mongo.SundayMember;
import org.itboys.core.entity.mongo.SundayStore;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/7/3
 * @Description：门店service
 */
@Service
public class SundayStoreService extends BaseMongoService<SundayStore> {
    @Autowired
    private ResourceHolder resourceHolder;
    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }
    @Override
    protected Class<SundayStore> getEntityClass() {
        return SundayStore.class;
    }
    @Autowired
    private SundayUploadService uploadService;
    @Autowired
    private SundayMemberService memberService;
    /**
     * 保存实体店
     * @param store 实体
     * @param imageFile 文件file
     * @throws Exception
     */
    public void save(SundayMember member,SundayStore store,MultipartFile imageFile) {
        Map<String, String> map = null;
        try {
            map = uploadService.uploadSingle(imageFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(StringUtils.isNotEmpty(map.get(SundayUploadService.UPLOAD_HALF_PATH))){
            store.setImage(map.get(SundayUploadService.UPLOAD_HALF_PATH));
        }
        store.setMemberName(member.getRealName());
        store.setMemberId(member.getId());
        store.setMobile(member.getMobile());
        if(store.getId() == 0L){
            super.save(store);
        }else{
            super.updateExceptEmpty(store.getId(),store);
        }
    }
    public PageResult<SundayStore> select(HttpServletRequest request, SystemUser user){
    //判断登录用户身份，如果是admin用户查询所有的
        boolean isSupper=user.getIsSuper()==1?true:false;
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request);
        Map<String, String> vagueParam = ParamUtil.packageMongoVagueParam(request, "name","mobile");
        param.put("isDeleted", SundayCommonConstants.NO);
        PageResult<SundayStore> sundayStorePageResult;
        if(isSupper){
            sundayStorePageResult = super.containpageQuery(request, vagueParam, param);
        }else {
            SundayMember member = memberService.getMemberByUserId(user.getId());
            if(member==null)throw new ErrorException(ExceptionEnum.USER_NONENTITY.getValue());
            param.put("memberId",member.getId());
            sundayStorePageResult = super.containpageQuery(request, vagueParam, param);
        }
        return sundayStorePageResult;
    }
}
